package com.edu.scnu.element;

import java.awt.Graphics;
import java.awt.Image;
import java.util.List;

import javax.swing.ImageIcon;

import com.edu.scnu.manager.ElementManager;
import com.edu.scnu.manager.GameElement;
import com.edu.scnu.show.GameJFrame;

public class GameItem extends ElementObj {

    private int type;

    public GameItem() {
    }

    @Override
    public ElementObj createElement(String str) {
        String[] split = str.split(",");
        this.setX(Integer.parseInt(split[0]));
        this.setY(Integer.parseInt(split[1]));
        ImageIcon icon2 = new ImageIcon("image/prop/" + split[2] + ".png");
        Image originalImage = icon2.getImage();
        int width = icon2.getIconWidth() * 3 / 4;
        int height = icon2.getIconHeight() * 3 / 4;
        Image scaleImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        ImageIcon newIcon = new ImageIcon(scaleImage);
        this.type = Integer.parseInt(split[2]);
        this.setW(width);
        this.setH(height);
        this.setIcon(newIcon);
        this.setSpeed(3);
        return this;
    }

    /**
     * 将元素显示在屏幕上
     */
    @Override
    public void showElement(Graphics g) {
        g.drawImage(this.getIcon().getImage(), this.getX(), this.getY(), this.getW(), this.getH(), null);
    }

    /**
     * 向下移动
     */
    @Override
    protected void move(long gameTime) {
        if (this.getY() > GameJFrame.GameY) {
            this.setLive(false);
            this.setCanRmove(true);
            return;
        }
        if (Play.score > 70 && this.getSpeed() < 4) {
            this.setSpeed(this.getSpeed() + 1);
        } else if (Play.score > 100 && this.getSpeed() < 5) {
            this.setSpeed(this.getSpeed() + 1);
        } else if (Play.score > 150 && this.getSpeed() < 6) {
            this.setSpeed(this.getSpeed() + 1);
        }
        this.setY(this.getY() + this.getSpeed());
    }

    @Override
    protected void updateImage(long gameTime) {

    }

    @Override
    public void die(long gameTime) {
        Play.score++;
        List<ElementObj> player = ElementManager.getManager().getElementsByKey(GameElement.PLAY);
        switch (this.type) {
            case 1:
            case 2:
            case 3:
                for (ElementObj p : player) {
                    if (p.getHp() < 4) {
                        p.setHp(p.getHp() + 1);
                    } else {
                        Play.score++;
                    }
                }
                break;
            case 4:
                Play.score++;
                break;
            case 5:
                for (ElementObj p : player) {
                    if (p.getHp() < 5) {
                        p.setHp(p.getHp() + 1);
                    } else {
                        Play.score++;
                    }
                }
                break;
            case 6:
                for (ElementObj p : player) {
                    if (p instanceof Play && ((Play) p).getShotTime() > 10) {
                        ((Play) p).setShotTime(((Play) p).getShotTime() - 2);
                    } else {
                        Play.score++;
                    }
                }
            case 7:
                for (ElementObj p : player) {
                    if (p.getSpeed() < 7) {
                        p.setSpeed(p.getSpeed() + 2);
                    } else {
                        Play.score++;
                        p.setSpeed(p.getSpeed() - 2);
                    }
                }
                break;

            default:
                break;
        }
    }

}
